package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
//model layer package has to be imported

/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressCtr
{
    private PersonContainer pc;
    private Person p;
    //declare instance variables to reference containers
    //passed as arguments from the main class
    
    //Address container is to be passed as parameter from the main controller
    public AddressCtr()
    {
        pc = new PersonContainer();
    }
    
    //Dummy implementation: after implementing the model layer 
    //objects of class Person are to be returned, not String
    public Person findPerson(int _id)
    {
        return pc.findPerson(_id);
    }
    
    
    public void createPerson(int _id, String _name, String _address, int _zipCode, String _city, String _phone)
    {   
        if(pc.findPerson(_id) != null)
        {
            if(pc.findPerson(_id).getId() == _id)
            {
                System.out.println("Id already exists.");
            }
            else
            {
                Person p = pc.createPerson(_id, _name, _address, _zipCode, _city, _phone);
                pc.addPerson(p);
                System.out.println("Done.");
            }
        }
        else
        {
             Person p = pc.createPerson(_id, _name, _address, _zipCode, _city, _phone);
             pc.addPerson(p);
             System.out.println("Done.");
        }
    }
    public void deletePerson(int id)
    {
        pc.deletePerson(id);
    }
    
    public void updatePerson(int _id, String _name, String _address, String _zipCode, String _city, String _phoneNr)
    {
        Person p = pc.findPerson(_id);
        if(!test(_name, ""))
        {
            p.setName(_name);
        }
        if(!test(_address, ""))
        {
            p.setAddress(_address);
        }
        if(!test(_zipCode, ""))
        {
            p.setZipCode(Integer.parseInt(_zipCode));
        }
        if(!test(_city, ""))
        {
            p.setCity(_city);
        }
        if(!test(_phoneNr, ""))
        {
            p.setPhoneNr(_phoneNr);
        }
    }
    private boolean test(String x, String y)
    {
        boolean result = false;
        if(x.equals(y))
        {
            result = true;
        }
        return result;
    }
    
    public ArrayList<String> listAllPersons()
    {
        return pc.showAllPersons();
    }
    
    public int getPersonID(int _id)
    {
        Person pId = pc.findPerson(_id);
        return pId.getId();
    }
}
